// pages/updatechange/updatechange.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    index: 0, id: 0,
    modalHidden: true,
    addtell: {
      addtellHidden: true, //弹出框显示/隐藏,
    }
  }, 
  bindViewTap: function (event) {
    console.log(event.currentTarget.dataset.index)
    this.setData({
      modalHidden: !this.data.modalHidden,
      index: event.currentTarget.dataset.index,
      id: event.currentTarget.dataset.id
    })
    var isNoShowCancel = this.data.list[this.data.id].isNoShowCancel
    console.log("isNoShowCancel = " + isNoShowCancel)
    if (isNoShowCancel == true) {
      this.setData({
        isNoShowCancel: isNoShowCancel,
        text: '确定'
      })
    } else {
      this.setData({
        isNoShowCancel: isNoShowCancel,
        text: '通过'
      })
    }
    console.log(this.data.list[this.data.id].info[this.data.index])
    this.setData({
      addtell: {
        empno: this.data.list[this.data.id].info[this.data.index].info.empno,
        ename: this.data.list[this.data.id].info[this.data.index].info.ename,
        ordeptno: this.data.list[this.data.id].info[this.data.index].info.ordeptno,
        orjob: this.data.list[this.data.id].info[this.data.index].info.orjob,
        newdeptno: this.data.list[this.data.id].info[this.data.index].info.newdeptno,
        newjob: this.data.list[this.data.id].info[this.data.index].info.newjob,
        reason: this.data.list[this.data.id].info[this.data.index].info.reason,
        addtellHidden: false,
      }
    })

  },

  modalBindaconfirm: function () {
    this.setData({
      modalHidden: !this.data.modalHidden
    })
  },
  modalBindcancel: function () {
    this.setData({
      modalHidden: !this.data.modalHidden
    })
  },
  kindToggle: function (e) {
    var id = e.currentTarget.id, list = this.data.list;
    // 使用id获取打开的子列表
    for (var i = 0, len = list.length; i < len; ++i) {
      if (list[i].id == id) {
        list[i].open = !list[i].open
      } else {
        list[i].open = false
      }
    }
    this.setData({
      list: list
    });
  },
  //通过按钮
  modalConfirm: function () {
    var that = this;
    var info = this.data.list[this.data.id].info[this.data.index].info;
    console.log(info)
    if (this.data.list[this.data.id].msg == "处理") {
      wx.request({
        url: 'http://192.168.1.6:8080/WeChatJSON//updateChangeLog',
        data: {
          id: info.id,
          flag: 1,
          empno: info.empno,
          ordeptno: info.ordeptno,
          newdeptno:info.newdeptno,
          ename:info.ename,
          reason:info.reason,
          job:info.newjob
        },
        method: 'GET',
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          console.log(res)
          if (res.data == "已处理"){
            wx.showToast({
              title: '已通过',
              icon:'success'
            })
          }
          that.getAllInfo(that)
        }
      })
    }
    //弹出框确认操作
    this.setData({
      addtell: {
        addtellHidden: true,
      }
    });
  },
  //驳回按钮
  modalCancel: function () {
    var info = this.data.list[this.data.id].info[this.data.index].info;
    var that = this;
    wx.request({
      url: 'http://192.168.1.6:8080/WeChatJSON//updateChangeLog',
      data: {
        id: info.id,
        flag: -1,
        empno: info.empno,
        ordeptno: info.ordeptno,
        newdeptno: info.newdeptno,
        ename: info.ename,
        reason: info.reason,
        job: info.newjob
      },
      method: 'GET',
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log(res)
        if (res.data == "已处理") {
          wx.showToast({
            title: '已驳回',
            icon: 'success'
          })
        }
        that.getAllInfo(that)
      }
    })
    //弹出框取消操作
    this.setData({
      addtell: {
        addtellHidden: true,
      }
    })

  },
  getAllInfo: function (_this) {
    var that = _this;
    wx.request({
      url: 'http://192.168.1.6:8080/WeChatJSON//allChange',
      data: {
      },
      method: 'GET',
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log(res)
        var info = []; var info1 = [];
        var options = []; var options1 = [];
        for (var i = 0; i < res.data.length; i++) {
          if (res.data[i].maflag == 1 && res.data[i].peflag == 0){
            options.push(res.data[i].ename + 
            " [ " + res.data[i].orjob + " ==> " + res.data[i].newjob+" ] ");
            info.push({
              info: res.data[i]
            })
          } else if (res.data[i].maflag == 1 && res.data[i].peflag != 0){
            options1.push(res.data[i].ename +
              " [ " + res.data[i].orjob + " ==> " + res.data[i].newjob + " ] ");
            info1.push({
              info: res.data[i]
            })
          }
          that.setData({
            list: [
              {
                id: 0,
                name: '待处理',
                open: false,
                options: options,
                msg: '处理',
                info: info,
                isNoShowCancel: false
              }, {
                id: 1,
                name: '已处理',
                open: false,
                options: options1,
                msg: '查看',
                info: info1,
                isNoShowCancel: true
              },]
          })
        }
      }
    })
  },
  onLoad: function (options) {
    this.getAllInfo(this)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getAllInfo(this)
  },

})